package com.liwbn.rbac.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.liwbn.rbac.service.FramemenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liwbn.rbac.dao.PermissionMapper;
import com.liwbn.rbac.model.FramemenuExample;
import com.liwbn.rbac.model.Permission;
import com.liwbn.rbac.model.PermissionExample;
import com.liwbn.rbac.service.PermissionService;
import com.liwbn.rbac.vo.SideBar;

@Service
public class PermissionServiceImp implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private FramemenuService framemenuService;

    @Override
    public Long insert(Permission model) {
        return permissionMapper.insert(model);
    }

    @Override
    public int insertSelective(Permission model) {
        return permissionMapper.insertSelective(model);
    }

    @Override
    public int updateByPrimaryKey(Permission model) {
        return permissionMapper.updateByPrimaryKey(model);
    }

    @Override
    public int updateByPrimaryKeySelective(Permission model) {
        return permissionMapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectByExample(null);
    }

    @Override
    public void updateByJSONModule(List<List<Permission>> menus) {
        List<Permission> addMenus = menus.get(0);
        List<Permission> updateMenus = menus.get(1);
        List<Permission> delMenus = menus.get(2);
        if (addMenus.size() > 0) {
            permissionMapper.insertBatch(addMenus);
        }
        if (updateMenus.size() > 0) {
            for (Permission f : updateMenus) {
                permissionMapper.updateByPrimaryKeySelective(f);
            }
        }
        if (delMenus.size() > 0) {
            List<Long> ids = new ArrayList<Long>();
            for (Permission f : delMenus) {
                ids.add(f.getId());
            }
            PermissionExample example = new PermissionExample();
            PermissionExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(ids);
            permissionMapper.deleteByExample(example);
        }
    }

    @Override
    public List<SideBar> selectPermission() {
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.addCriterion("a.parentId IS NOT NULL");
        example.setOrderByClause("orders");
        List<SideBar> childSideBar = permissionMapper
                .selectAllPermissionByExample(example);
        FramemenuExample framemenuExample = new FramemenuExample();
        FramemenuExample.Criteria framemenuCriteria = framemenuExample
                .createCriteria();
        framemenuCriteria.andParentidIsNull();
        framemenuExample.setOrderByClause("orders");
        List<SideBar> parent = framemenuService.selectParentSideBar();
        if (parent.size() > 0 && childSideBar.size() > 0) {
            Map<Long, List<SideBar>> childMap = new HashMap<Long, List<SideBar>>();
            Long parentId;
            List<SideBar> temp;
            for (SideBar sb : childSideBar) {
                parentId = Long.parseLong(sb.getParentid());
                temp = childMap.get(parentId);
                sb.setNocheck(true);
                if (temp == null) {
                    temp = new ArrayList<SideBar>();
                    temp.add(sb);
                    childMap.put(parentId, temp);
                } else {
                    temp.add(sb);
                }
            }
            for (SideBar sb : parent) {
                parentId = sb.getId();
                temp = childMap.get(parentId);
                sb.setNocheck(true);
                if (temp != null) {
                    sb.setChildren(temp);
                }
            }
        }
        return parent;
    }

    @Override
    public List<Long> selectMenuIdByRoles(List<Long> ids) {
        return permissionMapper.selectMenuIdByRoles(ids);
    }
}
